<?php
class Administrator_NewsController extends AdministratorController implements IController{

/**
	 * News_Model
	 * @var Administrator_News_Model
	 */
	protected $_model;	
	/* (non-PHPdoc)
	 * @see AdministratorController::indexAction()
	*/
	public function indexAction() {
		$this->view()->newsCount = $this->_model->countAllNews();
		$this->view()->newsCatCount = $this->_model->countAllNewsCat();
		$this->view()->top10New = $this->_model->getTopNewestNews($this->getContext());
		$this->view()->topHotNew = $this->_model->getTopHotNews($this->getContext());
	}
	
	public function initData() {
		$this->view()->viewBag['newsCat'] = $this->_model->getNewsCatForCombo();
	}
	
	/* (non-PHPdoc)
	 * @see AdministratorController::initSearch()
	 */
	public function initSearch() {
		$this->view()->viewBag['newsCat'] = ArrayUltils::insertAtFirstCombo($this->_model->getNewsCatForCombo());
	}

	public function validateData($data) {
		$result = array();
		$errors = array();
		if($data['name'] == null ||$data['name'] == ""){
			array_push($errors, "Bạn phải nhập tên");
		}
		if(count($errors)>0){
			$result['isError'] = true;
			$result['errors'] = $errors;
		}
		else {
			$result['isError'] = false;
			$result['errors'] = null;
		}
		$result['entity'] = $data;
		return $result;
	}
	/* (non-PHPdoc)
	 * @see AdministratorController::getSearchQuery()
	*/
	protected function getSearchQuery($se) {
		$sql = 'SELECT p.id, p.name,p.image,p.hit, 
				c.name as '.News::CATNAME.',
				s.name as '.News::STATUSNAME.'
				FROM news p
				LEFT JOIN newscat as c on p.newscat = c.id
				LEFT JOIN status as s on p.status = s.id
				 where p.id != 0 ';
	 	if(isset($se[News::ID])&& $se[News::ID] != "")
			$sql .= " AND p." . News::ID ."='".$se[News::ID]."'";
		if(isset($se[News::NAME])&& $se[News::NAME] != "")
			$sql .= " AND p." . News::NAME ." like '%".$se[News::NAME]."%'";
		
		if(isset($se[News::NEWSCAT])&& $se[News::NEWSCAT] != "")
			$sql .= " AND p." . News::NEWSCAT ."='".$se[News::NEWSCAT]."'";
		if(isset($se[Constant::ORDERBY])){
			$sql .=" order by p." . $se[Constant::ORDERBY];
			if(isset($se[Constant::ORDERAS]))
				$sql .= " " . $se[Constant::ORDERAS];
			else
				$sql .= " ASC";
		}
		$page = $se[Constant::PAGE];
		$size = $se[Constant::SIZE];
		$sql .= " limit " . (($page - 1) * $size) . "," . $size;
		return $sql;
	}
	
	/* (non-PHPdoc)
	 * @see AdministratorController::getCountQuery()
	*/
	protected function getCountQuery($se) {
		$sql = 'SELECT count(p.id) as '.Constant::TOTAL.' FROM news p where p.id != 0';
		if(isset($se[News::ID])&& $se[News::ID] != "")
			$sql .= " AND p." . News::ID ."='".$se[News::ID]."'";
		if(isset($se[News::NAME])&& $se[News::NAME] != "")
			$sql .= " AND p." . News::NAME ." like '%".$se[News::NAME]."%'";
		
		if(isset($se[News::NEWSCAT])&& $se[News::NEWSCAT] != "")
			$sql .= " AND p." . News::NEWSCAT ."='".$se[News::NEWSCAT]."'";
		return $sql;
	}
	
	/* (non-PHPdoc)
	 * @see AdministratorController::serializeSearchEntity()
	*/
	protected function serializeSearchEntity() {
		$searchEntity = parent::serializeSearchEntity();
		if(!isset($searchEntity[Constant::ORDERBY]))
			$searchEntity[Constant::ORDERBY] = News::NAME;
		return $searchEntity;
	}	
	public function beforeSave($obj) {
		$product = $this->_model->getByID($obj);
		if(isset($_FILES['News_image']) && $_FILES['News_image']["size"]>0){
			$imageName = $obj[News::NAME] + $obj[News::ID];
			$obj[News::IMAGE] = $this->uploadImage(News::IMAGE, $imageName);
		}
		else{
			if(isset($product))
				$obj[News::IMAGE] = $product[News::IMAGE];
		}
		return $obj;
	}
	/* (non-PHPdoc)
	 * @see AdministratorController::createNewObject()
	*/
	public function createNewObject() {
		$obj = parent::createNewObject();
		$obj[News::ORDINAL] = 10;
		return $obj;
	}
}